package edu.gyc.hiseckill.service.impl;

import edu.gyc.hiseckill.code.SeqUtil;
import edu.gyc.hiseckill.error.BusinessErrorEnum;
import edu.gyc.hiseckill.error.BusinessException;
import edu.gyc.hiseckill.model.Item;
import edu.gyc.hiseckill.model.UserInfo;
import edu.gyc.hiseckill.model.UserOrder;
import edu.gyc.hiseckill.dao.UserOrderDao;
import edu.gyc.hiseckill.service.ItemService;
import edu.gyc.hiseckill.service.UserInfoService;
import edu.gyc.hiseckill.service.UserOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2020-01-28
 */
@Service
public class UserOrderServiceImpl extends ServiceImpl<UserOrderDao, UserOrder> implements UserOrderService {

    @Autowired
    private ItemService itemService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserOrderService userOrderService;

    @Override
    @Transactional
    public UserOrder createOrder(UserOrder userOrder) throws BusinessException {
        //校验下单状态，下单商品是否存在，用户是否合法，购买数量是否正确
        Item item = itemService.getItem(userOrder.getItemId());
        if (item == null) {
            throw new BusinessException(BusinessErrorEnum.PARAMETER_VALIDATION_ERROR, "商品编号不存在");
        }

        UserInfo userInfo = userInfoService.getById(userOrder.getUserId());
        if (userInfo == null) {
            throw new BusinessException(BusinessErrorEnum.PARAMETER_VALIDATION_ERROR, "用户编号不存在");
        }

         int buyCount=userOrder.getAmount();
        if (buyCount <= 0) {
            throw new BusinessException(BusinessErrorEnum.PARAMETER_VALIDATION_ERROR, "商品购买数量不正确");
        }

        //校验活动id
        Integer promoId=userOrder.getPromoId();
        if (promoId!= null) {
            if (promoId != item.getPromo().getId()) {
                throw new BusinessException(BusinessErrorEnum.PARAMETER_VALIDATION_ERROR, "活动商品ID不正确");
            }else if(item.getPromo().getStatus()!=2){ //校验活动是否正在进行
                throw new BusinessException(BusinessErrorEnum.PARAMETER_VALIDATION_ERROR, "活动未开始或已结束");
            }
        }





        //下单减库存
        itemService.decreaseStock(userOrder.getItemId(), userOrder.getAmount());
        //增加商品销量
        itemService.increaseSales(userOrder.getItemId(), userOrder.getAmount());

        if (promoId != null) {
            userOrder.setItemPrice(item.getPromo().getPromoItemPrice());

        }else {
            userOrder.setItemPrice(item.getPrice());
        }
        userOrder.setTotalPrice(userOrder.getItemPrice().multiply(new BigDecimal(userOrder.getAmount())));




        //生产订单流水号
        userOrder.setId(makeOrderNumer());

        //订单入库
        userOrderService.save(userOrder);



        return userOrder;
    }

    private String makeOrderNumer() {
        //订单号有16位,前8位时间信息，中间6位自增序列，后2位分库分表位
        StringBuilder stringBuilder=new StringBuilder();
     //   LocalDateTime now =LocalDateTime.now();
      // String nowDate= now.format(DateTimeFormatter.ISO_DATE).replace("-","");
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmssSSS");
        stringBuilder.append(dateFormat.format(new Date()));

        stringBuilder.append(SeqUtil.generateOrderCode());
       // stringBuilder.append("00");

        return stringBuilder.toString();
    }
}
